﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Content;

namespace Megaman
{

    class StageSelect : IDisposable, CutsceneHandler
    {
        private Texture2D stageSelectTexture;
        private StageManager stageManager;
        public KeyboardState state;
        public ContentManager Content
        {
            get { return content; }
        }
        private ContentManager content;
        private int tileWidth = 150;
        private int tileHeight = 113;
        private int screenWidth = 1280;
        private int screenHeight = 720;
        private int xoffset = 50;
        private int yoffset = 50;
        private Texture2D restricted;
        private SpriteFont font;
        private Vector2[] StagePositions;
        public int index = 0;
        public Texture2D selector;
        private int mugshotWidth = 100;
        private int mugshotHeight = 100;
        private Texture2D bossIntroBackground;

        private int fullBossWidth = 400;
        private int fullBossHeight = 431;
        Song song;
        List<Cutscene> cutscenes;

        bool playingBackgroundMusic = false;
        bool playingBossIntroMusic = false;
        int cutsceneIndex = 0;
        bool playingCutscene;

        public StageSelect(StageManager stageManager, IServiceProvider service)
        {
            content = new ContentManager(service);
            content.RootDirectory = "Content";
            this.stageManager = stageManager;
            cutscenes = new List<Cutscene>();
            stageSelectTexture = Content.Load<Texture2D>("Stages/StageSelect/StageSelect");
            MediaPlayer.IsRepeating = true;
            song = Content.Load<Song>("Stages/StageSelect/StageSelectSong");
  
            font = Content.Load<SpriteFont>("Intro/Hud");
            cutscenes.Add(new Cutscene(service, "Cutscenes/StageSelect/Whyrestricted", this));
            restricted = content.Load<Texture2D>("Stages/StageSelect/restricted");
            selector = content.Load<Texture2D>("Stages/StageSelect/selector");
            bossIntroBackground = content.Load<Texture2D>("Stages/StageSelect/bossIntroBackground");

            StagePositions = new Vector2[8];

            StagePositions[0] = new Vector2(xoffset, yoffset);
            StagePositions[4] = new Vector2((screenWidth / 2) - (xoffset + tileWidth), (screenHeight / 2) - (tileHeight + yoffset));
            StagePositions[5] = new Vector2((screenWidth / 2) + (xoffset), (screenHeight / 2) - (tileHeight + yoffset));
            StagePositions[1] = new Vector2((screenWidth) - (xoffset + tileWidth), (yoffset));

            StagePositions[2] = new Vector2(xoffset, screenHeight - (tileHeight + yoffset));
            StagePositions[6] = new Vector2((screenWidth / 2) - (xoffset + tileWidth), (screenHeight / 2) + (yoffset));
            StagePositions[7] = new Vector2((screenWidth / 2) + (xoffset), (screenHeight / 2) + (yoffset));
            StagePositions[3] = new Vector2((screenWidth) - (xoffset + tileWidth), screenHeight - (tileHeight + yoffset));

        }

        public float time = 0;
        private float startTime = 8f;
        private bool chosen = false;
 
        public void Update(GameTime gameTime)
        {
            if (chosen)
            {
                if (!playingBossIntroMusic)
                {
 
                        MediaPlayer.IsRepeating = false;
                        MediaPlayer.Play(Content.Load<Song>("Stages/StageSelect/StageStart"));
                    if(MediaPlayer.State == MediaState.Playing)
                        playingBossIntroMusic = true;

                 } 

                
                startTime -= (float)gameTime.ElapsedGameTime.TotalSeconds;
            }

            else if (playingCutscene)
            {
                cutscenes[cutsceneIndex].Update(gameTime);
            }
            else
            {
                if (!playingBackgroundMusic)
                {
                    MediaPlayer.Stop();
                    MediaPlayer.Play(song);
                        if(MediaPlayer.State == MediaState.Playing )
                            playingBackgroundMusic = true;
                }
                HandleInput(gameTime);
            }

            if (startTime < 0)
                stageManager.LoadStage(index);
        }

        float waitTime = 0.5f;
        bool whyshown = false;
        public void HandleInput(GameTime gameTime)
        {
            state = Keyboard.GetState();
            GamePadState gpad = GamePad.GetState(PlayerIndex.One);

            waitTime -= (float)gameTime.ElapsedGameTime.TotalSeconds;
            if (waitTime < 0)
            {
                float movement = gpad.ThumbSticks.Left.X * 1.0f;

                if (state.IsKeyDown(Keys.Right) || movement > 0.5f)
                {

                    
                    waitTime = 0.2f;
                    
                 
                        index++;
                        if (index == 8)
                        {
                            index = 0;
                        }
                    
                    



                }

                if (state.IsKeyDown(Keys.Left) || movement < -0.5f)
                {
                   
                    waitTime = 0.2f;
 
                        index--;
                        if (index == -1)
                        {
                            index = 7;

                        }





                }
                if (state.IsKeyDown(Keys.Enter) || gpad.IsButtonDown(Buttons.Start))
                {
                    waitTime = 01f;
                    if (!stageManager.stages[index].Available)
                    {
                        if (!whyshown)
                        {
                            whyshown = true;
                            playingCutscene = true;
                        }
                    }
                    else
                    {

                        MediaPlayer.Stop();
                        chosen = true;
                    }

                }
            }

        }
        public void Draw(SpriteBatch spriteBatch, GameTime gameTime)
        {
            time += (float)gameTime.ElapsedGameTime.TotalSeconds;
            int i = 0;
            Texture2D icon = null;
            Texture2D iconSelected = null;
            spriteBatch.Begin();


            spriteBatch.Draw(stageSelectTexture, new Rectangle(0, 0, 1280, 720), Color.White);

            if (chosen)
                startBossIntro(gameTime, spriteBatch);
            else if (playingCutscene)
                cutscenes[cutsceneIndex].Draw(spriteBatch, gameTime);
            
            else
            {

                foreach (Vector2 pos in StagePositions)
                {


                    if (stageManager.stages[i].Available == true)
                    {
                        icon = stageManager.stages[i].icon;
                        iconSelected = stageManager.stages[i].iconSelected;
                    }
                    else
                        icon = restricted;

                    if (index == i && stageManager.stages[i].Available)
                    {
                        spriteBatch.Draw(iconSelected, new Rectangle((int)StagePositions[i].X, (int)StagePositions[i].Y, tileWidth + 30, tileHeight + 30), Color.White);
                        spriteBatch.DrawString(font, stageManager.stages[i].Name, new Vector2(StagePositions[i].X, StagePositions[i].Y + 133), Color.White);
                        spriteBatch.Draw(selector, new Rectangle((int)StagePositions[i].X - 100, (int)StagePositions[i].Y - 100, 200, 200), Color.White);
                        int count = stageManager.stages[i].bosses.Count;
                        if (count == 1)
                            spriteBatch.DrawString(font, "Defending Boss", new Vector2(screenWidth / 2 - (80), screenHeight - yoffset), Color.White);
                        else if (count > 1)
                            spriteBatch.DrawString(font, "Defending Bosses", new Vector2(screenWidth / 2 - (90), screenHeight - yoffset), Color.White);
                        int j = 0;
                        int leftmostx = 0;
                        foreach (Boss boss in stageManager.stages[i].bosses)
                        {
                            if (count % 2 == 0)
                            {
                                if (j == 0)
                                {
                                    leftmostx += (count / 2) * (mugshotWidth + xoffset);
                                    leftmostx -= xoffset / 2;
                                    j++;
                                }
                                else
                                    leftmostx -= (mugshotWidth + xoffset);

                            }
                            //Caso impar de bosses, para dibujarlos en la mitad
                            else
                            {
                                if (j == 0)
                                {
                                    leftmostx = mugshotWidth / 2;
                                    leftmostx += ((count) / 2) * (mugshotWidth + xoffset);
                                    j++;
                                }
                                else
                                {
                                    leftmostx -= (mugshotWidth + xoffset);
                                }


                            }
                            spriteBatch.Draw(boss.bossMugshot, new Rectangle(screenWidth / 2 - leftmostx, screenHeight - (mugshotHeight + yoffset), mugshotWidth, mugshotHeight), Color.White);
                            count--;
                        }
                    }
                    else
                    {
                        spriteBatch.Draw(icon, new Rectangle((int)StagePositions[i].X, (int)StagePositions[i].Y, tileWidth, tileHeight), Color.White);
                        spriteBatch.DrawString(font, stageManager.stages[i].Name, new Vector2(StagePositions[i].X, StagePositions[i].Y + 113), Color.White);

                    }
                    i++;
                }
            }


            spriteBatch.End();
        }
        private int AlphaValue = 1;
        private int fadeIncrement = 3;
        float fadeDelay = 0.035f;

        public void startBossIntro(GameTime gameTime, SpriteBatch spriteBatch)
        {
            if (AlphaValue < 255)
            {
                fadeDelay -= (float)gameTime.ElapsedGameTime.TotalSeconds;

                if (fadeDelay < 0)
                {
                    fadeDelay = 0.035f;

                    AlphaValue += fadeIncrement;
                }
            }
            if (AlphaValue > 255)
                AlphaValue = 255;


            spriteBatch.Draw(bossIntroBackground, new Rectangle(0, 0, 1280, 720), new Color(255, 255, 255, (byte)AlphaValue));
            int count = stageManager.stages[index].bosses.Count;
          
            int j = 0;
            int leftmostx = 0;
            foreach (Boss boss in stageManager.stages[index].bosses)
            {
                if (count % 2 == 0)
                {
                    if (j == 0)
                    {
                        leftmostx += (count / 2) * (fullBossWidth - 30);
                        leftmostx -= xoffset / 2;
                        j++;
                    }
                    else
                        leftmostx -= (fullBossWidth -30);

                }
                //Caso impar de bosses, para dibujarlos en la mitad
                else
                {
                    if (j == 0)
                    {
                        leftmostx = fullBossWidth / 2;
                        leftmostx += ((count) / 2) * (fullBossWidth -30);
                        j++;
                    }
                    else
                    {
                        leftmostx -= (fullBossWidth - 30);
                    }


                }
                spriteBatch.Draw(boss.bossFull, new Rectangle(screenWidth / 2 - leftmostx, screenHeight / 2 - fullBossHeight/2, fullBossWidth, fullBossHeight), new Color(255, 255, 255, (byte)AlphaValue));
                spriteBatch.DrawString(font, boss.name, new Vector2(screenWidth / 2 - leftmostx + 150, screenHeight / 2 - fullBossHeight / 2), new Color(255, 255, 255, (byte)AlphaValue));
                count--;
            }
        }


        #region IDisposable Members

        public void Dispose()
        {
            Content.Unload();
        }

        #endregion

        #region CutsceneHandler Members

        public void CutsceneEnd()
        {
            cutscenes[cutsceneIndex].Dispose();
            cutsceneIndex++;
            if (cutsceneIndex == cutscenes.Count)
                cutsceneIndex=-1;
            playingCutscene = false;
            playingBackgroundMusic = false;
        }

        #endregion
    }
}
